package com.itheima.java.ai.langchin4j.controller;

import com.itheima.java.ai.langchin4j.entity.Doctor;
import com.itheima.java.ai.langchin4j.entity.DoctorSchedule;
import com.itheima.java.ai.langchin4j.server.DoctorScheduleService;
import com.itheima.java.ai.langchin4j.server.DoctorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/doctors")
@Tag(name = "医生管理", description = "医生信息和排班管理相关接口")
public class DoctorController {
    
    @Autowired
    private DoctorService doctorService;
    
    @Autowired
    private DoctorScheduleService doctorScheduleService;
    
    @GetMapping("/departments")
    @Operation(summary = "获取所有科室")
    public Map<String, Object> getAllDepartments() {
        Map<String, Object> response = new HashMap<>();
        try {
            List<String> departments = doctorService.getAllDepartments();
            response.put("success", true);
            response.put("data", departments);
            response.put("message", "获取科室列表成功");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取科室列表失败: " + e.getMessage());
        }
        return response;
    }
    
    @GetMapping
    @Operation(summary = "获取医生列表")
    public Map<String, Object> getDoctors(@RequestParam(required = false) String department) {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Doctor> doctors;
            if (department != null && !department.isEmpty()) {
                doctors = doctorService.getDoctorsByDepartment(department);
            } else {
                doctors = doctorService.getAllActiveDoctors();
            }
            response.put("success", true);
            response.put("data", doctors);
            response.put("message", "获取医生列表成功");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取医生列表失败: " + e.getMessage());
        }
        return response;
    }
    
    @GetMapping("/{doctorId}")
    @Operation(summary = "获取医生详情")
    public Map<String, Object> getDoctorById(@PathVariable("doctorId") Long doctorId) {
        Map<String, Object> response = new HashMap<>();
        try {
            Doctor doctor = doctorService.getDoctorById(doctorId);
            if (doctor != null) {
                response.put("success", true);
                response.put("data", doctor);
                response.put("message", "获取医生详情成功");
            } else {
                response.put("success", false);
                response.put("message", "医生不存在");
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取医生详情失败: " + e.getMessage());
        }
        return response;
    }
    
    @GetMapping("/{doctorId}/schedules")
    @Operation(summary = "获取医生排班")
    public Map<String, Object> getDoctorSchedules(
            @PathVariable("doctorId") Long doctorId,
            @RequestParam String date) {
        Map<String, Object> response = new HashMap<>();
        try {
            LocalDate scheduleDate = LocalDate.parse(date);
            List<DoctorSchedule> schedules = doctorScheduleService.getDoctorScheduleByDate(doctorId, scheduleDate);
            response.put("success", true);
            response.put("data", schedules);
            response.put("message", "获取医生排班成功");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取医生排班失败: " + e.getMessage());
        }
        return response;
    }
    
    @GetMapping("/schedules/available")
    @Operation(summary = "获取指定日期可预约的排班")
    public Map<String, Object> getAvailableSchedules(@RequestParam String date) {
        Map<String, Object> response = new HashMap<>();
        try {
            LocalDate scheduleDate = LocalDate.parse(date);
            List<DoctorSchedule> schedules = doctorScheduleService.getAvailableSchedulesByDate(scheduleDate);
            response.put("success", true);
            response.put("data", schedules);
            response.put("message", "获取可预约排班成功");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取可预约排班失败: " + e.getMessage());
        }
        return response;
    }
}